package com.sheyu.mall.product.service.sku.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.product.dao.sku.SkuInfoDao;
import com.sheyu.mall.product.entity.sku.SkuInfoEntity;
import com.sheyu.mall.product.service.sku.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    /**
     * key:
     * catelogId: 0
     * brandId: 0
     * min: 0
     * max: 0
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.eq("sku_id",key).or().like("sku_name",key);
        }

        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId)&&Integer.parseInt(brandId)>0){
            wrapper.eq("brand_id",brandId);
        }

        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotBlank(catelogId)&&Integer.parseInt(catelogId)>0){
            wrapper.eq("catalog_id",catelogId);
        }

        String maxValue = (String) params.get("max");
        if (StringUtils.isNotBlank(maxValue)){
            BigDecimal bigDecimal = new BigDecimal(maxValue);
            if (bigDecimal.compareTo(new BigDecimal("0"))==1) {
                wrapper.le("price", maxValue);
            }
        }

        String minValue = (String) params.get("min");
        if (StringUtils.isNotBlank(minValue)){
            wrapper.ge("price",minValue);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> skuInfoEntities = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return skuInfoEntities;
    }


}
